package com.fdd.spring.proxyexercise;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class CalProxyProvider {
    private Cal cal;
    public CalProxyProvider(Cal cal) {
        this.cal = cal;
    }


    public Cal getProxy(){

         return (Cal) Proxy.newProxyInstance(this.getClass().getClassLoader(), this.cal.getClass().getInterfaces(), (proxy1, method, args) -> {
             Object result = null;
             try {
                System.out.println("方法执行开始-日志-方法名" + method.getName() + "参数" + Arrays.asList(args));//横切关注点-前置通知
                result = method.invoke(cal, args);
                System.out.println("方法执行正常结束,结果是" + result + "方法名" + method.getName() + "参数" + Arrays.asList(args));//横切关注点-返回通知
            }  catch (Exception e) {
                e.printStackTrace();
                //如果执行方法时发生异常,横切关注点-异常通知
                System.out.println("方法执行异常结束,异常信息是" + e.getClass().getName() + "方法名" + method.getName() + "参数" + Arrays.asList(args));
            } finally {//横切关注点-最终通知
                System.out.println("方法执行结束,方法名" + method.getName() + "参数" + Arrays.asList(args));
            }

             return result;
         });
    }
}
